home *** CD-ROM | disk | FTP | other *** search
- Path: info.spt.net.cn!usenet
- From: Xu Ji <jafd@public.sta.net.cn>
- Newsgroups: comp.lang.c++
- Subject: Re: Q: Returning a reference
- Date: Mon, 15 Jan 1996 09:58:57 -0800
- Organization: ZheJiang Securities Co.Ltd
- Message-ID: <30FA95E1.50C@public.sta.net.cn>
- References: <4cvsm2$5ig@dub-news-svc-4.compuserve.com>
- NNTP-Posting-Host: ts1-20.sta.net.cn
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0b5 (Win16; I)
-
- Martin Aupperle wrote:
- :
- : Borland C++ V4.5 does not allow to return a reference to a local
- : variable:
- :
- : int &doIt() {
- :
- : int i = 7;
- : return i; // syntax error
- : }
- :
- : I remember that I once had a compiler that did allow it (it gave me
- : only a warning).
- : Which one is right? I think that it should be an error because after
- : the function has terminated, the reference has no data object it is
- : bound to any more.
- :
- : Martin
- :
- : -----------------------------------
- : Signatures are a waste of bandwidth
- : -----------------------------------
-
- I think it should be an error, and I test it under Borland C++
- and Watcom C++, both this two compiler report it as an error.
-
- but the following function can be compiled:
-
- int &doIt()
- {
- int i = 7;
- return (int &)i;
- }
-
- Best wishs,
- Xu yifeng
-